#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->Init();

    QStringList strList1;
    strList1<<"125Kbps"<<"200Kbps"<<"250Kbps"<<"500Kbps"<<"1000Kbps";
    ui->comboBox->addItems(strList1);

    thread_1 = new QThread;
    recharge = new ReCharge;
    recharge->moveToThread(thread_1);
    thread_1->start();

    //创建线程对象
    task2 = new QThread;               //task2:CAN操作
    //创建任务对象
    can = new canThread;
    //放入多线程
    can->moveToThread(task2);
    //启动线程
    task2->start();
    connect(this,&MainWindow::SIGtocancon,can,&canThread::openCan);  //点击启动CAN按钮打开CAN
    connect(this,&MainWindow::SIGtocansto,can,&canThread::closeCan);  //点击启动CAN按钮打开CAN
    connect(this,&MainWindow::SIGcanbps,can,&canThread::RceiveKbps); //波特率参数传递
    connect(this,&MainWindow::SIGtocansen,can,&canThread::sendData); //点击发送按钮将框内ID和Data数据送到CAN发送函数
    connect(can,&canThread::SIGCanOpen,this,&MainWindow::CANconnect);
    connect(can,&canThread::SIGCanClose,this,&MainWindow::CANdisconnect);
    connect(can,&canThread::dataCan,recharge,&ReCharge::data1);
    connect(can,&canThread::stoSenData,ui->textBrowser,&QTextEdit::append); //显示CAN发送数据
    connect(recharge,&ReCharge::sig_state,this,&MainWindow::ViewState);
    connect(recharge,&ReCharge::sig_messige,this,&MainWindow::ViewMessage);
    connect(recharge,&ReCharge::sig_model,this,&MainWindow::ViewModel);
    connect(recharge,&ReCharge::sig_version,this,&MainWindow::ViewVersion);
    connect(recharge,&ReCharge::sig_data1,this,&MainWindow::Viewdata1);
    connect(recharge,&ReCharge::sig_data2,this,&MainWindow::Viewdata2);
    connect(recharge,&ReCharge::sig_serial,this,&MainWindow::ViewSerial);

    timer1.setTimerType(Qt::PreciseTimer);
    connect(&timer1,&QTimer::timeout,this,[=]()
    {
        VisitState();//访问模块状态
        VisitMessage();//访问模块信息
        VisitData1();//访问模块模拟量1
        VisitData2();//访问模块模拟量2
        //VisitGroup();//访问模块分组
    });
    timer1.start(50);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::Init()
{
    ui->lightCAN->setGray();
    ui->lightON->setGray();
    ui->lightSleep->setGray();
    ui->lightSlowUp->setGray();
    ui->lightUpdate->setGray();
    ui->lightLimitW->setGray();
    ui->lightLimitA->setGray();
    ui->lightOnekey->setGray();
}

void MainWindow::CANconnect()
{
    ui->lightCAN->setGreen();
}

void MainWindow::CANdisconnect()
{
    ui->lightCAN->setRed();
}

void MainWindow::VisitState()
{
    int ID;
    ID = 0x1805FF80;
    QString Data = "0100000000000000";
    std::vector<int> vec;
    QByteArray data1 = Data.toUpper().toLatin1();
    for(int i=0; i<8; i++)
    {
        vec.push_back(Data.mid(2*i,2).toInt(0,16));
        double vec = QRandomGenerator::global()->generateDouble();
    }
    emit SIGtocansen(ID,vec);
}

void MainWindow::VisitMessage()
{
    int ID;
    ID = 0x1805FF80;
    QString Data = "0200000000000000";
    std::vector<int> vec;
    QByteArray data1 = Data.toUpper().toLatin1();
    for(int i=0; i<8; i++)
    {
        vec.push_back(Data.mid(2*i,2).toInt(0,16));
        double vec = QRandomGenerator::global()->generateDouble();
    }
    emit SIGtocansen(ID,vec);
}

void MainWindow::VisitModel()
{
    int ID;
    ID = 0x1805FF80;
    QString Data = "0300000000000000";
    std::vector<int> vec;
    QByteArray data1 = Data.toUpper().toLatin1();
    for(int i=0; i<8; i++)
    {
        vec.push_back(Data.mid(2*i,2).toInt(0,16));
        double vec = QRandomGenerator::global()->generateDouble();
    }
    emit SIGtocansen(ID,vec);
}

void MainWindow::VisitVersion()
{
    int ID;
    ID = 0x1805FF80;
    QString Data = "0400000000000000";
    std::vector<int> vec;
    QByteArray data1 = Data.toUpper().toLatin1();
    for(int i=0; i<8; i++)
    {
        vec.push_back(Data.mid(2*i,2).toInt(0,16));
        double vec = QRandomGenerator::global()->generateDouble();
    }
    emit SIGtocansen(ID,vec);
}

void MainWindow::VisitSerial()
{
    int ID;
    ID = 0x1805FF80;
    QString Data = "0500000000000000";
    std::vector<int> vec;
    QByteArray data1 = Data.toUpper().toLatin1();
    for(int i=0; i<8; i++)
    {
        vec.push_back(Data.mid(2*i,2).toInt(0,16));
        double vec = QRandomGenerator::global()->generateDouble();
    }
    emit SIGtocansen(ID,vec);
}

void MainWindow::VisitData1()
{
    int ID;
    ID = 0x1805FF80;
    QString Data = "0600000000000000";
    std::vector<int> vec;
    QByteArray data1 = Data.toUpper().toLatin1();
    for(int i=0; i<8; i++)
    {
        vec.push_back(Data.mid(2*i,2).toInt(0,16));
        double vec = QRandomGenerator::global()->generateDouble();
    }
    emit SIGtocansen(ID,vec);
}

void MainWindow::VisitData2()
{
    int ID;
    ID = 0x1805FF80;
    QString Data = "0700000000000000";
    std::vector<int> vec;
    QByteArray data1 = Data.toUpper().toLatin1();
    for(int i=0; i<8; i++)
    {
        vec.push_back(Data.mid(2*i,2).toInt(0,16));
        double vec = QRandomGenerator::global()->generateDouble();
    }
    emit SIGtocansen(ID,vec);
}

void MainWindow::VisitGroup()
{
    int ID;
    ID = 0x1805FF80;
    QString Data = "0800000000000000";
    std::vector<int> vec;
    QByteArray data1 = Data.toUpper().toLatin1();
    for(int i=0; i<8; i++)
    {
        vec.push_back(Data.mid(2*i,2).toInt(0,16));
        double vec = QRandomGenerator::global()->generateDouble();
    }
    emit SIGtocansen(ID,vec);
}

void MainWindow::ViewState(QString da)
{
    QString byte1 = da.mid(0,2);
    QString byte2 = da.mid(2,4);
    QString byte3 = da.mid(6,4);
    QString byte4 = da.mid(10,6);
    //状态指示灯
    if(byte1 == "01")
    {
        ui->lightON->setGreen();
        ui->lightSleep->setGray();
        ui->lightUpdate->setGray();
        ui->lightSlowUp->setGray();
        ui->lightLimitW->setGray();
        ui->lightLimitA->setGray();
        ui->lightOnekey->setGray();
    }
    if(byte1 == "02")
    {
        ui->lightON->setRed();
        ui->lightSleep->setGray();
        ui->lightUpdate->setGray();
        ui->lightSlowUp->setGray();
        ui->lightLimitW->setGray();
        ui->lightLimitA->setGray();
        ui->lightOnekey->setGray();
    }
    if(byte1 == "04")
    {
        ui->lightSlowUp->setGreen();
        ui->lightON->setGray();
        ui->lightSleep->setGray();
        ui->lightUpdate->setGray();
        ui->lightLimitW->setGray();
        ui->lightLimitA->setGray();
        ui->lightOnekey->setGray();
    }
    if(byte1 == "08")
    {
        ui->lightSleep->setGreen();
        ui->lightSlowUp->setGray();
        ui->lightON->setGray();
        ui->lightUpdate->setGray();
        ui->lightLimitW->setGray();
        ui->lightLimitA->setGray();
        ui->lightOnekey->setGray();
    }
    if(byte1 == "10")
    {
        ui->lightUpdate->setGreen();
        ui->lightSleep->setGray();
        ui->lightSlowUp->setGray();
        ui->lightON->setGray();
        ui->lightLimitW->setGray();
        ui->lightLimitA->setGray();
        ui->lightOnekey->setGray();
    }
    if(byte1 == "20")
    {
        ui->lightLimitW->setGreen();
        ui->lightUpdate->setGray();
        ui->lightSleep->setGray();
        ui->lightSlowUp->setGray();
        ui->lightON->setGray();
        ui->lightLimitA->setGray();
        ui->lightOnekey->setGray();
    }
    if(byte1 == "40")
    {
        ui->lightLimitA->setGreen();
        ui->lightLimitW->setGray();
        ui->lightUpdate->setGray();
        ui->lightSleep->setGray();
        ui->lightSlowUp->setGray();
        ui->lightON->setGray();
        ui->lightOnekey->setGray();
    }
    if(byte1 == "80")
    {
        ui->lightOnekey->setGreen();
        ui->lightLimitA->setGray();
        ui->lightLimitW->setGray();
        ui->lightUpdate->setGray();
        ui->lightSleep->setGray();
        ui->lightSlowUp->setGray();
        ui->lightON->setGray();
    }

    //输出电压电流
    float putV = (byte2.mid(2,2)+byte2.mid(0,2)).toInt(0,16)/10.0;
    ui->lineEdit_putV->setText(QString::number(putV)+"V");
    float putA = (byte3.mid(2,2)+byte3.mid(0,2)).toInt(0,16)/10.0;
    ui->lineEdit_putA->setText(QString::number(putA)+"A");

    //故障信息
    QString byt1 = QString("%1").arg(byte4.mid(0,2).toInt(0,16), 8, 2, QLatin1Char('0'));//状态2
    QString byt1_0 = byt1.mid(0,1);//交流输入欠压
    QString byt1_1 = byt1.mid(1,1);//市电缺相
    QString byt1_2 = byt1.mid(2,1);//交流输入过压
    QString byt1_3 = byt1.mid(3,1);//输出过压
    QString byt1_4 = byt1.mid(4,1);//输出过流
    QString byt1_5 = byt1.mid(5,1);//散热器故障
    QString byt1_6 = byt1.mid(6,1);//风扇故障
    QString byt1_7 = byt1.mid(7,1);//BUS不平衡
    if(byt1_0 == "0")
    {
        ui->Button_jlsrqy->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt1_0 == "1")
    {
        ui->Button_jlsrqy->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
    if(byt1_1 == "0")
    {
        ui->Button_sdqx->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt1_1 == "1")
    {
        ui->Button_sdqx->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
    if(byt1_2 == "0")
    {
        ui->Button_jlsrgy->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt1_2 == "1")
    {
        ui->Button_jlsrgy->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
    if(byt1_3 == "0")
    {
        ui->Button_scgy->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt1_3 == "1")
    {
        ui->Button_scgy->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
    if(byt1_4 == "0")
    {
        ui->Button_scgl->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt1_4 == "1")
    {
        ui->Button_scgl->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
    if(byt1_5 == "0")
    {
        ui->Button_srqgz->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt1_5 == "1")
    {
        ui->Button_srqgz->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
    if(byt1_6 == "0")
    {
        ui->Button_fsgz->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt1_6 == "1")
    {
        ui->Button_fsgz->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
    if(byt1_7 == "0")
    {
        ui->Button_BUSbph->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt1_7 == "1")
    {
        ui->Button_BUSbph->setStyleSheet("background-color: rgb(255, 0, 0);");
    }

    QString byt2 = QString("%1").arg(byte4.mid(2,2).toInt(0,16), 8, 2, QLatin1Char('0'));//状态1
    QString byt2_0 = byt2.mid(0,1);//输出欠压
    QString byt2_1 = byt2.mid(1,1);//主变原边过流
    QString byt2_2 = byt2.mid(2,1);//Sci通讯异常
    QString byt2_3 = byt2.mid(3,1);//PFC BUS过压
    QString byt2_4 = byt2.mid(4,1);//PFC BUS欠压
    QString byt2_5 = byt2.mid(5,1);//缓启动异常
    QString byt2_6 = byt2.mid(6,1);//均流异常
    QString byt2_7 = byt2.mid(7,1);//PFC输入过流
    if(byt2_0 == "0")
    {
        ui->Button_scqy->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt2_0 == "1")
    {
        ui->Button_scqy->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
    if(byt2_1 == "0")
    {
        ui->Button_zbybgl->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt2_1 == "1")
    {
        ui->Button_zbybgl->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
    if(byt2_2 == "0")
    {
        ui->Button_sci->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt2_2 == "1")
    {
        ui->Button_sci->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
    if(byt2_3 == "0")
    {
        ui->Button_pfcbusgy->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt2_3 == "1")
    {
        ui->Button_pfcbusgy->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
    if(byt2_4 == "0")
    {
        ui->Button_pfcbusqy->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt2_4 == "1")
    {
        ui->Button_pfcbusqy->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
    if(byt2_5 == "0")
    {
        ui->Button_hqdyc->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt2_5 == "1")
    {
        ui->Button_hqdyc->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
    if(byt2_6 == "0")
    {
        ui->Button_jlyc->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt2_6 == "1")
    {
        ui->Button_jlyc->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
    if(byt2_7 == "0")
    {
        ui->Button_pfcsrgl->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt2_7 == "1")
    {
        ui->Button_pfcsrgl->setStyleSheet("background-color: rgb(255, 0, 0);");
    }

    QString byt3 = QString("%1").arg(byte4.mid(4,2).toInt(0,16), 8, 2, QLatin1Char('0'));//状态0
    QString byt3_0 = byt3.mid(0,1);//EEPROM故障
    QString byt3_1 = byt3.mid(1,1);//交流频率异常
    QString byt3_2 = byt3.mid(2,1);//三相输入不平衡
    QString byt3_3 = byt3.mid(3,1);//交流输入断
    QString byt3_4 = byt3.mid(4,1);//校正失败
    QString byt3_5 = byt3.mid(5,1);//输出过载
    QString byt3_6 = byt3.mid(6,1);//Can通讯异常
    QString byt3_7 = byt3.mid(7,1);//保留
    if(byt3_0 == "0")
    {
        ui->Button_eepro->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt3_0 == "1")
    {
        ui->Button_eepro->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
    if(byt3_1 == "0")
    {
        ui->Button_jlplyc->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt3_1 == "1")
    {
        ui->Button_jlplyc->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
    if(byt3_2 == "0")
    {
        ui->Button_sxsrbph->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt3_2 == "1")
    {
        ui->Button_sxsrbph->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
    if(byt3_3 == "0")
    {
        ui->Button_jlsrd->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt3_3 == "1")
    {
        ui->Button_jlsrd->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
    if(byt3_4 == "0")
    {
        ui->Button_jzsb->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt3_4 == "1")
    {
        ui->Button_jzsb->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
    if(byt3_5 == "0")
    {
        ui->Button_scgz->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt3_5 == "1")
    {
        ui->Button_scgz->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
    if(byt3_6 == "0")
    {
        ui->Button_cantxyc->setStyleSheet("background-color: rgb(0, 255, 0);");
    }else if(byt3_6 == "1")
    {
        ui->Button_cantxyc->setStyleSheet("background-color: rgb(255, 0, 0);");
    }

}

void MainWindow::ViewMessage(QString da)
{
    QString data1 = da.mid(2,4);
    QString data2 = da.mid(6,4);
    QString data3 = da.mid(10,2);
    QString data4 = da.mid(12,2);
    int lowV = (data1.mid(2,2)+data1.mid(0,2)).toInt()/10;
    ui->lineEdit_putlowV->setText(QString::number(lowV)+"V");
    int highV = (data2.mid(2,2)+data2.mid(0,2)).toInt()/10;
    ui->lineEdit_puthighV->setText(QString::number(highV)+"V");
    int lowA = data3.toInt();
    ui->lineEdit_putlowA->setText(QString::number(lowA)+"A");
    int highA = data4.toInt();
    ui->lineEdit_puthighA->setText(QString::number(highA)+"A");
}

void MainWindow::ViewModel(QString da)
{
    QString data1;
    if(da.mid(0,14) == "5A58441E54F501")
    {
        data1 = "ZXD30T501";
    }
    float num = da.mid(14,2).toInt()/10.0;
    ui->lineEdit_model->setText(data1+QString::number(num));
}

void MainWindow::ViewVersion(QString da)
{
    QString data1 = da.mid(0,4);
    QString data2 = da.mid(4,2);
    QString data3 = da.mid(6,2);
    QString data4 = da.mid(8,4);
    QString data5 = da.mid(12,2);
    QString data6 = da.mid(14,2);
    int numq1 = (data1.mid(2,2)+data1.mid(0,2)).toInt();
    int numq2 = data2.toInt();
    int numq3 = data3.toInt();
    ui->lineEdit_qdate->setText(QString::number(numq1)+" "+QString::number(numq2)+" "+QString::number(numq3));
    int numh1 = (data4.mid(2,2)+data4.mid(0,2)).toInt();
    int numh2 = data5.toInt();
    int numh3 = data6.toInt();
    ui->lineEdit_hdate->setText(QString::number(numh1)+" "+QString::number(numh2)+" "+QString::number(numh3));
}

void MainWindow::Viewdata1(QString da)
{
    QString data1 = da.mid(0,4);
    QString data2 = da.mid(4,4);
    QString data3 = da.mid(8,4);
    QString data4 = da.mid(12,4);
    float temEN = (data1.mid(2,2)+data1.mid(0,2)).toInt()/10.0;
    ui->lineEdit_ENTem->setText(QString::number(temEN)+"℃");
    float temDC = (data2.mid(2,2)+data2.mid(0,2)).toInt()/10.0;
    ui->lineEdit_DCTem->setText(QString::number(temDC)+"℃");
    float temPFC = (data3.mid(2,2)+data3.mid(0,2)).toInt()/10.0;
    ui->lineEdit_ENTem->setText(QString::number(temPFC)+"℃");
    int numFS = (data3.mid(2,2)+data3.mid(0,2)).toInt()/10;
    ui->lineEdit_FSspeed->setText(QString::number(numFS));
}

void MainWindow::Viewdata2(QString da)
{
    QString data1 = da.mid(2,4);
    QString data2 = da.mid(6,4);
    QString data3 = da.mid(10,4);
    float abV = (data1.mid(2,2)+data1.mid(0,2)).toInt()/10.0;
    ui->lineEdit_AB_V->setText(QString::number(abV)+"V");
    float bcV = (data2.mid(2,2)+data2.mid(0,2)).toInt()/10.0;
    ui->lineEdit_BC_V->setText(QString::number(bcV)+"V");
    float caV = (data3.mid(2,2)+data3.mid(0,2)).toInt()/10.0;
    ui->lineEdit_CA_V->setText(QString::number(caV)+"V");
}

void MainWindow::ViewGroup(QString da)
{

}

void MainWindow::ViewSerial(QString da)
{
    QString data1 = da.mid(8,8);
    QString spacedData;
    for(int i=0; i<data1.length(); i+=2)
    {
        spacedData += data1.mid(i, 2);
        spacedData += " ";
    }
    ui->lineEdit_serial->setText(spacedData);
}


void MainWindow::on_pushButton_open_clicked()
{
    int ID;
    ID = 0x0803FF80;
    QString Data = "00000000AA000000";
    std::vector<int> vec;
    QByteArray data1 = Data.toUpper().toLatin1();
    for(int i=0; i<8; i++)
    {
        vec.push_back(Data.mid(2*i,2).toInt(0,16));
        double vec = QRandomGenerator::global()->generateDouble();
    }
    emit SIGtocansen(ID,vec);
}


void MainWindow::on_pushButton_close_clicked()
{
    int ID;
    ID = 0x0803FF80;
    QString Data = "0000000055000000";
    std::vector<int> vec;
    QByteArray data1 = Data.toUpper().toLatin1();
    for(int i=0; i<8; i++)
    {
        vec.push_back(Data.mid(2*i,2).toInt(0,16));
        double vec = QRandomGenerator::global()->generateDouble();
    }
    emit SIGtocansen(ID,vec);
}


void MainWindow::on_ButtonCANON_clicked()
{
    QString str = ui->comboBox->currentText();
    QString result = str.left(str.length() - 4);
    int num = result.toInt();
    SIGcanbps(num);
    SIGtocancon();
}


void MainWindow::on_Button_CANOFF_clicked()
{
    SIGtocansto();
}


void MainWindow::on_pushButton_set_clicked()
{
    int ID;
    ID = 0x1004FF80;
    QString data1 = "00000000";
    int daV = ui->lineEdit_setV->text().toInt();
    QString strV = QString("%1").arg(daV*10, 4,16,QLatin1Char('0'));
    int daA = ui->lineEdit_setA->text().toInt();
    QString strA = QString("%1").arg(daA*100, 4,16,QLatin1Char('0'));
    QString Data = data1+strV.mid(2,2)+strV.mid(0,2)+strA.mid(2,2)+strA.mid(0,2);
    std::vector<int> vec;
    //QByteArray data1 = Data.toUpper().toLatin1();
    for(int i=0; i<8; i++)
    {
        vec.push_back(Data.mid(2*i,2).toInt(0,16));
        double vec = QRandomGenerator::global()->generateDouble();
    }
    emit SIGtocansen(ID,vec);
}


void MainWindow::on_pushButton_open_2_clicked()
{
    VisitModel();//访问模块型号
    VisitVersion();//访问模块软件版本
    VisitSerial();//访问模块序列号
}

